﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MedicalDB;
using ZedGraph;
using NHibernateDB;

namespace MedicalDB
{
    public partial class GraficLumina : Form
    {

        DateTime dataInceput;
        DateTime dataSfarsit ;
        int idsenzor;
        string CNP = string.Empty;
        GraphPane myPane;
        ZedGraphControl zgc;
        int idImagine = 0;
        List<string> namesForm;

        public GraficLumina(DateTime dataInceput, DateTime dataSfarsit, int idsenzor, string CNP,string limba)
        {
            InitializeComponent();
            this.dataInceput = dataInceput;
            this.dataSfarsit = dataSfarsit;
            this.idsenzor = idsenzor;
            this.CNP = CNP;
            this.zgc = zg1;

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            namesForm = xmlParser.ExtractAttribute("GraficLumina", string.Empty, string.Empty, "name");

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CreateGraph();
            SetSize();
        }


        public void CreateGraph()
        {
            GraphPane myPane = zgc.GraphPane;
            this.myPane = myPane;

            // Set the titles and axis labels

            PersistenceManager pm = PersistenceManager.Instance;
            IList<DateGeneralePacient> listdate = pm.RetrieveEquals<DateGeneralePacient>("IdPacient", CNP);

            myPane.Title.Text = namesForm[0] + listdate[0].Nume + " " + listdate[0].Prenume;
            myPane.XAxis.Title.Text = namesForm[1] + dataInceput.ToString() + namesForm[2] + dataSfarsit.ToString();
            myPane.YAxis.Title.Text = namesForm[3];

            PointPairList list = new PointPairList();

            IList<SenzoriLumina> stList = pm.RetrieveEquals<SenzoriLumina>("IdPacient", CNP, "IdSenzor", idsenzor);

            DateTime dataAnterioara = DateTime.Today;
            Boolean first = true;
            int diff = 0;
            foreach (SenzoriLumina st in stList)
            {
                if ((st.Data.CompareTo(dataInceput) >= 0) && st.Data.CompareTo(dataSfarsit) <= 0)
                {

                    if (first)
                    {
                        list.Add(0, st.Lumina);
                        first = false;
                        dataAnterioara = st.Data;
                    }
                    else
                    {
                        TimeSpan ts = st.Data - dataAnterioara;
                        // Difference in days.
                        int differeneInseconds = ts.Seconds;
                        diff += differeneInseconds;
                        list.Add(diff, st.Lumina);
                        dataAnterioara = st.Data;
                    }

                }
            }


            // Generate a blue curve with circle symbols, and "My Curve 2" in the legend
            LineItem myCurve = myPane.AddCurve(namesForm[0], list, Color.Blue,
                                    SymbolType.Circle);
            // Fill the area under the curve with a white-red gradient at 45 degrees
            myCurve.Line.Fill = new Fill(Color.White, Color.Red, 45F);
            // Make the symbols opaque by filling them with white
            myCurve.Symbol.Fill = new Fill(Color.White);

            // Fill the axis background with a color gradient
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);

            // Fill the pane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);

            // Calculate the Axis Scale Ranges
            zgc.AxisChange();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            SetSize();
        }

        public string SaveImage()
        {
            Bitmap bmp = myPane.GetImage();
            string filename = "C:\\lumina" + (idImagine++) + ".jpg";
            bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
            return filename;
        }

        private void SetSize()
        {
            zg1.Location = new Point(10, 10);
            // Leave a small margin around the outside of the control
            zg1.Size = new Size(this.ClientRectangle.Width - 20, this.ClientRectangle.Height - 20);
        }
    }
}